package com.zklh.book.system.service.impl;

import com.zklh.book.system.common.enums.StatusEnum;
import com.zklh.book.system.model.Book;
import com.zklh.book.system.model.User;
import com.zklh.book.system.repo.BookRepo;
import com.zklh.book.system.repo.UserRepo;
import com.zklh.book.system.repo.impl.BookRepoImpl;
import com.zklh.book.system.repo.impl.UserRepoImpl;
import com.zklh.book.system.service.BookService;
import com.zklh.book.system.util.BeanFactory;

import java.util.List;

/**
 * 图书服务实现
 *
 * @author 坐看落花
 * @version 1.0
 * @date 2021/3/20 19:30
 */
public class BookServiceImpl implements BookService {

    private BookRepo bookRepo = BeanFactory.getBean(BookRepoImpl.class);
    private UserRepo userRepo = BeanFactory.getBean(UserRepoImpl.class);

    @Override
    public List<Book> getAll() {
        return bookRepo.findAll();
    }

    @Override
    public List<Book> getMy(User user){
        return user.getBorrowedBooks();
    }

    @Override
    public boolean borrow(User user, String bookCode) {
        Book book = bookRepo.get(bookCode);
        if (book == null) {
            throw new RuntimeException("书不存在");
        }
        if (!StatusEnum.ENABLE.getKey().equals(book.getStatus())) {
            throw new RuntimeException("书已经下架");
        }

        // 余量不足, 无法借出
        if (book.getRemain() < 1) {
            throw new RuntimeException("书余量不足");
        }

        // 用户已经借过该书, 也无法借出
        for (Book borrowedBook : user.getBorrowedBooks()) {
            if (borrowedBook.getCode().equals(book.getCode())) {
                throw new RuntimeException("不可以重复借同一本书");
            }
        }

        // 更新书
        book.setRemain(book.getRemain()-1);
        bookRepo.save(book);

        // 保存用户
        user.getBorrowedBooks().add(book);
        userRepo.save(user);

        return true;
    }

    @Override
    public void back(User user, String bookCode) {
        Book book = bookRepo.get(bookCode);
        if (book == null) {
            throw new RuntimeException("书不存在!");
        }

        boolean userHasBook = false;

        for (int i = 0; i < user.getBorrowedBooks().size(); i++) {
            if (user.getBorrowedBooks().get(i).getCode().equals(bookCode)) {
                user.getBorrowedBooks().remove(i);
                book.setRemain(book.getRemain()+1);
                userHasBook = true;
                break;
            }
        }
        if (!userHasBook) {
            throw new RuntimeException("用户没有借该书!");
        }

        bookRepo.save(book);
        userRepo.save(user);
    }

    @Override
    public boolean add(Book book) {
        // 如果书库已经有该书了, 则无法添加
        if (bookRepo.get(book.getCode()) != null) {
            throw new RuntimeException("该书已经存在, 不要重复创建!");
        }
        // 刚创建的时候 要是 disable的状态
        book.setStatus(StatusEnum.DISABLE.getKey());
        book.setRemain(book.getAmount());
        bookRepo.save(book);
        return true;
    }

    @Override
    public boolean enable(String bookCode) {
        Book book = bookRepo.get(bookCode);
        if (book == null) {
            throw new RuntimeException("书不存在!");
        }
        book.setStatus(StatusEnum.ENABLE.getKey());
        bookRepo.save(book);
        return true;
    }

    @Override
    public boolean disable(String bookCode) {
        Book book = bookRepo.get(bookCode);
        if (book == null) {
            throw new RuntimeException("书不存在!");
        }
        book.setStatus(StatusEnum.DISABLE.getKey());
        bookRepo.save(book);
        return true;
    }

    @Override
    public boolean delete(String bookCode) {
        Book book = bookRepo.get(bookCode);
        if (book == null) {
            throw new RuntimeException("书不存在!");
        }
        book.setStatus(StatusEnum.DELETED.getKey());
        bookRepo.save(book);
        return true;
    }

    @Override
    public boolean ccode(String bookcode,String name){
        Book book = bookRepo.get(bookcode);
        if(book==null){
            throw new RuntimeException("书不存在");
        }
        book.setName(name);
        bookRepo.save(book);
        return true;
    }
    @Override
    public Book getone(String bookCode){
        Book book = bookRepo.getone(bookCode);
        return book;
    }

    @Override
    public boolean numbering(String bookCode, String code) {
        Book book = bookRepo.get(bookCode);
        if(book==null){
            throw new RuntimeException("书不存在");
        }
        book.setCode(code);
        bookRepo.save(book);
        return true;
    }

    @Override
    public boolean numbers(String bookCode, Integer sum) {
        Book book = bookRepo.get(bookCode);
        if(book==null){
            throw new RuntimeException("书不存在");
        }
        book.setRemain(sum);
        bookRepo.save(book);
        return true;
    }


}
